﻿using Nutshell.Data;
using Nutshell.Distributing;
using Nutshell.Messaging.Models;
using Nutshell.NetMQ;
using Nutshell.Speech;
using Nutshell.Speech.Models;

namespace Nutshell.DSS.Business
{
        public class GlobalManager : NotifyPropertyChangedObject
        {
                #region 构造函数

                private GlobalManager()
                {
                }

                #endregion

                #region 单例

                /// <summary>
                ///         单例
                /// </summary>
                public static readonly GlobalManager Instance = new GlobalManager();

                #endregion

                public SendSite<LogMessageModel> LogSendSite { get; private set; }

                public ReceiveSite<StringValueMessageModel> SynthesizeSite { get; private set; }

                public SpeechSynthesizer Synthesizer { get; private set; }


                public void Start()
                {
                        StartLogSendSite();
                        StartSynthesizer();
                        StartSynthesizeReceiveSite();
                }

                private void StartLogSendSite()
                {
                        LogSendSite = NetMQSiteHelper.CreateSendSite<LogMessageModel>(@"配置\Log");
                        LogSendSite.Start();
                }

                private void StartSynthesizer()
                {
                        Synthesizer = new SpeechSynthesizer();
                        Synthesizer.Load<SpeechSynthesizerModel>(@"配置\Synthesizer.config");

                        Synthesizer.Started += (o, args) => LogSendSite.Send(new LogMessageModel(Synthesizer.Id + args.ToString()));

                        Synthesizer.Start();
                }

                private void StartSynthesizeReceiveSite()
                {
                        SynthesizeSite = NetMQSiteHelper.CreateReceiveSite<StringValueMessageModel>(@"配置\Synthesize");

                        SynthesizeSite.Dispatched += (o, args) => Synthesizer.SpeakSync(args.Value.Value);
                        SynthesizeSite.Start();
                }


                public void Stop()
                {
                        SynthesizeSite.Stop();
                        Synthesizer.Stop();
                        LogSendSite.Stop();
                }
        }
}